package com.mycompany.app;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException {
        String userDirectory = System.getProperty("user.dir");

        File currentDir = new File(userDirectory);
        List<String> directories = displayDirectory(currentDir);

        Collections.sort(directories, new Comparator<String>() {
            public int compare(String person, String person1) {
                return person1.compareTo(person);
            }
        });

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(directories);

        String fileName = "dir_info.json";
        File file = new File(fileName);
        file.delete();

        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
        writer.write(json);
        writer.close();
    }

    // Function displaying all the directories
    // present in the directory
    public static List<String> displayDirectory(File dir)
    {
        List<String> list = new ArrayList<>();
        File[] files = dir.listFiles();

        // For-each loop for iteration
        for (File file : files) {
            // Checking of file inside directory
            if (file.isDirectory()) {
                // Display directories inside directory
                list.add(file.getName());
            }
        }
        return list;
    }

}
